package com.ymatou.apigateway.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.OpenApiGetOrdersReq;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.service.OpenapiService;
import com.ymatou.apigateway.service.V1getorderCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * openapi获取订单列表FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_v1getorderlist {
	private static OpenapiReq openapiReq;
	private static OpenApiGetOrdersReq openApiGetOrdersReq;
	private static V1getorderCall v1getorderlistCall;
	private static OpenApiWapper openApiWapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("openapi获取订单列表");
		openApiWapper = new OpenApiWapper();
	}

	@Before
	public void caseUp() {

		openapiReq = new OpenapiReq();

		openapiReq.setMethod("ymatou.order.list.get");

		openApiGetOrdersReq = new OpenApiGetOrdersReq();
		v1getorderlistCall = new V1getorderCall();

	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(v1getorderlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1getorderlistCall_001() {
		Logger.start(true, "openapi获取订单列表 排序为1 升序");
		try {
			long sellerId = Long.valueOf(EnvSetup.getData("SellerId"));

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openApiGetOrdersReq.setDate_type(5);
			openApiGetOrdersReq.setEnd_date(YMTDateUtil.getSysTime());
			openApiGetOrdersReq.setOrder_status("3,2");
			openApiGetOrdersReq.setPage_no(1);
			openApiGetOrdersReq.setPage_rows(1);
			openApiGetOrdersReq.setNeeds_delivery_info(true);
			openApiGetOrdersReq.setSort_type(1);
			openApiGetOrdersReq.setStart_date(YMTDateUtil
					.getBeforeOrNextDay(-1));

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(openApiGetOrdersReq));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.order.list.get",
							app_id);

			v1getorderlistCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			v1getorderlistCall.setData(openapiReq);
			v1getorderlistCall.callService();

			Logger.verifyEquals("0000", v1getorderlistCall.getCode(), "校验code");
			Logger.verifyEquals("null", v1getorderlistCall.getMsg(),
					"校验message");

			Logger.verifyNotNull(v1getorderlistCall.getContent(), "校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1getorderlistCall_002() {
		Logger.start(true, "openapi获取订单列表  排序为0 倒序");
		try {
			long sellerId = Long.valueOf(EnvSetup.getData("SellerId"));

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openApiGetOrdersReq.setDate_type(1);
			openApiGetOrdersReq.setEnd_date(YMTDateUtil.getSysTime());
			openApiGetOrdersReq.setOrder_status("2");
			openApiGetOrdersReq.setPage_no(1);
			openApiGetOrdersReq.setPage_rows(10);
			openApiGetOrdersReq.setSort_type(0);
			openApiGetOrdersReq.setStart_date(YMTDateUtil
					.getBeforeOrNextDay(-5));

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(openApiGetOrdersReq));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.order.list.get",
							app_id);

			v1getorderlistCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			v1getorderlistCall.setData(openapiReq);
			v1getorderlistCall.callService();

			Logger.verifyEquals("0000", v1getorderlistCall.getCode(), "校验code");
			Logger.verifyEquals("null", v1getorderlistCall.getMsg(),
					"校验message");

			Logger.verifyNotNull(v1getorderlistCall.getContent(), "校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1getorderlistCall_003() {
		Logger.start(true, "openapi获取订单列表  订单状态为不存在的状态10");
		try {
			long sellerId = Long.valueOf(EnvSetup.getData("SellerId"));

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openApiGetOrdersReq.setDate_type(1);
			openApiGetOrdersReq.setEnd_date(YMTDateUtil.getSysTime());
			openApiGetOrdersReq.setOrder_status("10");
			openApiGetOrdersReq.setPage_no(1);
			openApiGetOrdersReq.setPage_rows(10);
			openApiGetOrdersReq.setSort_type(0);
			openApiGetOrdersReq.setStart_date(YMTDateUtil
					.getBeforeOrNextDay(-5));

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(openApiGetOrdersReq));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.order.list.get",
							app_id);

			v1getorderlistCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			v1getorderlistCall.setData(openapiReq);
			v1getorderlistCall.callService();

			Logger.verifyEquals("0000", v1getorderlistCall.getCode(), "校验code");
			Logger.verifyEquals("null", v1getorderlistCall.getMsg(),
					"校验message");

			DataUtil.verify(null,
					v1getorderlistCall.getContent().get("orders_info"),
					"校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1getorderlistCall_004() {
		Logger.start(true, "openapi获取订单列表  订单状态为null,默认查询所有状态数据");
		try {
			long sellerId = Long.valueOf(EnvSetup.getData("SellerId"));

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openApiGetOrdersReq.setDate_type(1);
			openApiGetOrdersReq.setEnd_date(YMTDateUtil.getSysTime());
			//openApiGetOrdersReq.setOrder_status("10");
			openApiGetOrdersReq.setPage_no(1);
			openApiGetOrdersReq.setPage_rows(10);
			openApiGetOrdersReq.setSort_type(0);
			openApiGetOrdersReq.setStart_date(YMTDateUtil
					.getBeforeOrNextDay(-5));

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(openApiGetOrdersReq));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.order.list.get",
							app_id);

			v1getorderlistCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			v1getorderlistCall.setData(openapiReq);
			v1getorderlistCall.callService();

			Logger.verifyEquals("0000", v1getorderlistCall.getCode(), "校验code");
			Logger.verifyEquals("null", v1getorderlistCall.getMsg(),
					"校验message");

			Logger.verifyNotNull(v1getorderlistCall.getContent(), "校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1getorderlistCall_005() {
		Logger.start(true, "openapi获取订单列表  同时传多个订单状态查询");
		try {
			long sellerId = Long.valueOf(EnvSetup.getData("SellerId"));

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openApiGetOrdersReq.setDate_type(1);
			openApiGetOrdersReq.setEnd_date(YMTDateUtil.getSysTime());
			openApiGetOrdersReq.setOrder_status("1,2,3");
			openApiGetOrdersReq.setPage_no(1);
			openApiGetOrdersReq.setPage_rows(10);
			openApiGetOrdersReq.setSort_type(0);
			openApiGetOrdersReq.setStart_date(YMTDateUtil
					.getBeforeOrNextDay(-5));

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(openApiGetOrdersReq));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.order.list.get",
							app_id);

			v1getorderlistCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			v1getorderlistCall.setData(openapiReq);
			v1getorderlistCall.callService();

			Logger.verifyEquals("0000", v1getorderlistCall.getCode(), "校验code");
			Logger.verifyEquals("null", v1getorderlistCall.getMsg(),
					"校验message");

			Logger.verifyNotNull(v1getorderlistCall.getContent(), "校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1getorderlistCall_006() {
		Logger.start(true, "openapi获取订单列表  请求分页页码 page_no 为null");
		try {
			long sellerId = Long.valueOf(EnvSetup.getData("SellerId"));

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openApiGetOrdersReq.setDate_type(1);
			openApiGetOrdersReq.setEnd_date(YMTDateUtil.getSysTime());
			openApiGetOrdersReq.setOrder_status("1,2,3");
			//openApiGetOrdersReq.setPage_no(1);
			openApiGetOrdersReq.setPage_rows(10);
			openApiGetOrdersReq.setSort_type(0);
			openApiGetOrdersReq.setStart_date(YMTDateUtil
					.getBeforeOrNextDay(-5));

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(openApiGetOrdersReq));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.order.list.get",
							app_id);

			v1getorderlistCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			v1getorderlistCall.setData(openapiReq);
			v1getorderlistCall.callService();

			Logger.verifyEquals("0001", v1getorderlistCall.getCode(), "校验code");
			Logger.verifyEquals("pageNum:请求页号不能小于1",
					v1getorderlistCall.getMsg(), "校验message");

			Logger.verifyIsNull(v1getorderlistCall.getContent(), "校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1getorderlistCall_007() {
		Logger.start(true, "openapi获取订单列表 ,根据接单时间 4 排序为0 降序");
		try {
			long sellerId = Long.valueOf(EnvSetup.getData("SellerId"));

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openApiGetOrdersReq.setDate_type(4);
			openApiGetOrdersReq.setEnd_date(YMTDateUtil.getSysTime());
			openApiGetOrdersReq.setOrder_status("4");
			openApiGetOrdersReq.setPage_no(1);
			openApiGetOrdersReq.setPage_rows(10);
			openApiGetOrdersReq.setSort_type(0);
			openApiGetOrdersReq.setStart_date(YMTDateUtil
					.getBeforeOrNextDay(-1));

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(openApiGetOrdersReq));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.order.list.get",
							app_id);

			v1getorderlistCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			v1getorderlistCall.setData(openapiReq);
			v1getorderlistCall.callService();

			Logger.verifyEquals("0000", v1getorderlistCall.getCode(), "校验code");
			Logger.verifyEquals("null", v1getorderlistCall.getMsg(),
					"校验message");

			Logger.verifyNotNull(v1getorderlistCall.getContent(), "校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1getorderlistCall_008() {
		Logger.start(true, "openapi获取订单列表 stag");
		try {
			String app_id = "tMAFkslclJcYOoPQj4";
			String app_secret = "4dmiCd8SGbNvO3f82X5WI7DWaQkHLz9V";
			String auth_code = "Lwz0QMFwyWCOpNIst4VARZ3aeLaVxMbI";

			openapiReq.setApp_id(app_id);

			openApiGetOrdersReq.setDate_type(2);
			openApiGetOrdersReq.setEnd_date(YMTDateUtil.getSysTime());
			openApiGetOrdersReq.setOrder_status("4,12,17");
			openApiGetOrdersReq.setPage_no(1);
			openApiGetOrdersReq.setPage_rows(10);
			openApiGetOrdersReq.setSort_type(1);
			openApiGetOrdersReq.setStart_date(YMTDateUtil
					.getBeforeOrNextDay(-180));

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(auth_code);
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(openApiGetOrdersReq));

			String url = String
					.format("http://open.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.order.list.get",
							app_id);

			v1getorderlistCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					app_secret));

			v1getorderlistCall.setData(openapiReq);
			v1getorderlistCall.callService();

			Logger.verifyEquals("0000", v1getorderlistCall.getCode(), "校验code");
			Logger.verifyEquals("null", v1getorderlistCall.getMsg(),
					"校验message");

			Logger.verifyNotNull(v1getorderlistCall.getContent(), "校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1getorderlistCall_009() {
		Logger.start(true, "openapi获取订单列表 stag");
		try {

			String app_id = "0Y6XVHaj7kjsoEcvGg";
			String app_secret = "5340AA34A5D6A154066F5074B31BA672149B88B399F6611450BB931952F3EE3277A049D2CAB92FF1E467219E19726988";
			String auth_code = "04MvSmEWQvF07g4U98uHsViKAJYlot5r";

			openapiReq.setApp_id(app_id);

			openApiGetOrdersReq.setDate_type(2);
			openApiGetOrdersReq.setEnd_date(YMTDateUtil.getSysTime());
			openApiGetOrdersReq.setOrder_status("12");
			openApiGetOrdersReq.setPage_no(1);
			openApiGetOrdersReq.setPage_rows(10);
			openApiGetOrdersReq.setSort_type(1);
			openApiGetOrdersReq.setStart_date(YMTDateUtil
					.getBeforeOrNextDay(-2));

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(auth_code);
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(openApiGetOrdersReq));

			String url = String
					.format("http://open.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.order.list.get",
							app_id);

			v1getorderlistCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			v1getorderlistCall.setData(openapiReq);
			v1getorderlistCall.callService();

			Logger.verifyEquals("0000", v1getorderlistCall.getCode(), "校验code");
			Logger.verifyEquals("null", v1getorderlistCall.getMsg(),
					"校验message");

			Logger.verifyNotNull(v1getorderlistCall.getContent(), "校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	/*	stag 东京彩虹桥 
		select * from application where seller_id=563971 ;
		select * from auth_code where seller_id=563971 ;*/
	public void Tc_V1getorderlistCall_010() {
		Logger.start(true, "openapi获取订单列表 stag");
		try {

			String app_id = "llzlHWWDTkEsUUjwKf";
			String app_secret = "23E5D6CE556F02F5B532DD0A819E4FAE00A8116D29F4870651650E4A6CD15949E9838B4A644122CA2B205386E1050D7E";
			String auth_code = "Ul1BpFlBHdLR6EnEv75RV6QeradgjdBk";

			openapiReq.setApp_id(app_id);

			openApiGetOrdersReq.setDate_type(2);
			openApiGetOrdersReq.setStart_date("2018-01-15 00:05:03");
			openApiGetOrdersReq.setOrder_status("3");
			openApiGetOrdersReq.setPage_no(1);
			openApiGetOrdersReq.setPage_rows(100);
			openApiGetOrdersReq.setSort_type(1);
			openApiGetOrdersReq.setEnd_date("2018-01-15 11:04:24");

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(auth_code);
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(openApiGetOrdersReq));

			String url = String
					.format("http://open.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.order.list.get",
							app_id);

			v1getorderlistCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			v1getorderlistCall.setData(openapiReq);
			v1getorderlistCall.callService();

			Logger.verifyEquals("0000", v1getorderlistCall.getCode(), "校验code");
			Logger.verifyEquals("null", v1getorderlistCall.getMsg(),
					"校验message");

			Logger.verifyNotNull(v1getorderlistCall.getContent(), "校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}